package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.annotation.Log;
import com.dongdongshop.data.Page;
import com.dongdongshop.mapper.TbItemCatMapper;
import com.dongdongshop.pojo.TbItemCat;
import com.dongdongshop.pojo.TbItemCatExample;
import com.dongdongshop.pojo.TbTypeTemplate;
import com.dongdongshop.service.TbItemCatService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Service
@org.springframework.stereotype.Service
public class TbItemCatServiceImpl implements TbItemCatService {

    @Autowired
    private TbItemCatMapper tbItemCatMapper;

    @Override
    @Log
    public Page<TbItemCat> getItemCatInfo(Integer pageNum, Integer pageSize, Long id) {
        PageHelper.startPage(pageNum,pageSize);
        TbItemCatExample tc = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = tc.createCriteria();
        criteria.andParentIdEqualTo(id);
        List<TbItemCat> itemCat = tbItemCatMapper.selectByExample(tc);
        PageInfo<TbItemCat> pageInfo = new PageInfo<>(itemCat);

        Page<TbItemCat> page = new Page<>(pageInfo.getPageNum(),pageInfo.getPageSize(),
                pageInfo.getTotal(),pageInfo.getList());
        return page;
    }

    @Override
    @Log
    public void insertItemCatInfo(TbItemCat tbItemCat) {
        tbItemCatMapper.insertSelective(tbItemCat);
    }

    @Override
    @Log
    public TbItemCat updateItemCat(Long id) {
        return tbItemCatMapper.selectByPrimaryKey(id);
    }

    @Override
    @Log
    public void updateItemCatInfo(TbItemCat tbItemCat) {
        tbItemCatMapper.updateByPrimaryKeySelective(tbItemCat);
    }

    @Override
    @Log
    public void deleteInfo(Long[] id) {
        tbItemCatMapper.deleteByIds(id);
    }

    @Override
    @Log
    public List<TbItemCat> sublevel(Long id) {
        if (id == 0){
            return null;
        }
        List list = new ArrayList();
        TbItemCat tbItemCat = tbItemCatMapper.selectByPrimaryKey(id);
        if (tbItemCat.getParentId() != 0){
            TbItemCat tbItemCat1 = tbItemCatMapper.selectByPrimaryKey(tbItemCat.getParentId());
            list.add(tbItemCat1);
        }
        list.add(tbItemCat);
        return list;
    }

    @Override
    public List<TbItemCat> getCatInfo1(Long parentId) {
        return tbItemCatMapper.selectByCat(parentId);
    }

    @Override
    public TbItemCat  getTypeId(Long id) {
        return tbItemCatMapper.selectByPrimaryKey(id);
    }

//    @Override
//    public List<TbItemCat> getCatInfo() {
//        return tbItemCatMapper.selectByExample(null);
//    }
}
